<?php

namespace App\Console\Commands\OpenShop;

use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Kuafu\File\FileUtil;
use Kuafu\File\PrivateFileUtil;

class RepairShopLocationFile extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:repairShopLocationFile';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店 - 门店选址 - 文件修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $this->repairOrderLogFile();

        return parent::SUCCESS;
    }

    public function repairOrderLogFile()
    {
        $client = new Client();
        $dir = formatDateYmd(time());
        OpenShopSignOrderLogModel::query()
            ->where("type", OpenShopSignOrderLogModel::UPLOAD_ORDER_PAY_PROOF)
            ->get()
            ->each(function($orderLog)use ($client, $dir){
                if($orderLog->changes && $changes = json_decode($orderLog->changes)){
                    if($changes->proofImg && strstr($changes->proofImg, FileUtil::domain())){
                        $key = str_replace(FileUtil::domain(), "", $changes->proofImg);
                        $path = $dir."/".trim($key, "/");
                        $tempData = $client->request("get", $changes->proofImg)->getBody()->getContents();
                        Storage::disk("public")->put($path, $tempData);

                        PrivateFileUtil::uploadFile(
                            storage_path("app/public/".$path),
                            explode(".", $key)[1],
                            trim($key, "/")
                        );

                        $changes->proofImg = trim($key, "/");
                        $orderLog->changes = json_encode((array)$changes, JSON_UNESCAPED_UNICODE);
                        $orderLog->save();
                        Storage::disk("public")->delete($path);
                    }
                }
            });
    }

    public function repairShopLocationFile()
    {
        $originalKey = [
            "d7/f3/3cd91ce393ec5c03437fe426a22eae0a9ff5942b41457d283adadf4e2f76.xlsx",
            "81/0b/e04933a34e51fb4f736e6fd4348695af738c4a3362b21d902f432b32ec40.xlsx"
        ];

        $client = new Client();
        $dir = formatDateYmd(time());
        foreach ($originalKey as $key){
            $path = $dir."/".$key;
            $tempData = $client->request("get", picturePath($key))->getBody()->getContents();
            Storage::disk("public")->put($key, $tempData);
            $arr = explode(".", $key);
            PrivateFileUtil::uploadFile(
                storage_path("app/public/".$path),
                $arr[1],
                $key
            );
        }
    }
}
